﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZhimeiSoft.Framework.Client.Control.DateTimeGrid
{
    public partial class MonthDateTimeForMonth : UserControl
    {
        private DateTime currDateTime;
        public DateTime MonthDateTime
        {
            get {
                return currDateTime;
            }
            set
            {
                currDateTime = value;
                initLabelDate(value);
                monthDateTimeForWeek.Tag = value.Ticks.ToString();
                monthDateTimeForWeek.SetDateTime(value);
            }
        }
        public EventCurrMonth OnEventCurrMonth;
        public EventClickCurrDay OnEventClickCurrDay
        {
            get
            {
                return monthDateTimeForWeek.OnEventClickCurrDay;
            }
            set
            {
                monthDateTimeForWeek.OnEventClickCurrDay += value;
            }
        }

        /// <summary>
        /// 当前所选择的日期
        /// </summary>
        public MonthDateTimeForDay CurrDateTimeViewDayControl
        {
            get
            {
                return monthDateTimeForWeek.currDateTimeViewControl;
            }
        }
        /// <summary>
        /// 是否显示月控制
        /// </summary>
        public bool ShowMonthControl
        {
            get
            {
                return panShowMonth.Visible;
            }
            set
            {
                panShowMonth.Visible = value;
            }
        }
        public MonthDateTimeForMonth()
        {
            InitializeComponent();
            
            if (MonthDateTime.Year == 1)
            {
                MonthDateTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month,
                                DateTime.Today.Day);
            }
            else
            {
                initLabelDate(MonthDateTime);
                monthDateTimeForWeek.Tag = MonthDateTime.Ticks.ToString();
                monthDateTimeForWeek.SetDateTime(MonthDateTime);
            }
        }
        /// <summary>
        /// 对数据进行绑定
        /// </summary>
        /// <param name="dsData">绑定的数据集</param>
        /// <param name="dispKey">显示值字段</param>
        /// <param name="valueKey">返回值字段</param>
        /// <param name="startTimeKey">时间开始字段</param>
        /// <param name="endTimeKey">结束字段</param>
        public void DataBindForMonth(DataSet dsData, string dispKey, string valueKey, string timeKey)
        {
            monthDateTimeForWeek.ClearBindData();
            monthDateTimeForWeek.SetDataBind(dsData, dispKey, valueKey, timeKey);
        }

        private void btnUpMonth_Click(object sender, EventArgs e)
        {
            changeDateMonth(-1);
            if (OnEventCurrMonth != null)
            {
                OnEventCurrMonth(MonthDateTime);
            }
        }

        private void btnNextMonth_Click(object sender, EventArgs e)
        {
            changeDateMonth(1);
            if (OnEventCurrMonth != null)
            {
                OnEventCurrMonth(MonthDateTime);
            }

        }

        private void initLabelDate(DateTime dt)
        {
            labelDateTime.Text = string.Concat(dt.Year, "年", dt.Month, "月");
        }

        private void changeDateMonth(int iMonthNumber)
        {
            if (monthDateTimeForWeek.Tag != null && monthDateTimeForWeek.Tag.ToString() != string.Empty)
            {
                string sTicks = monthDateTimeForWeek.Tag.ToString();
                DateTime currentDatetime = new DateTime(long.Parse(sTicks));
                currentDatetime = currentDatetime.AddMonths(iMonthNumber);
                MonthDateTime = new DateTime(currentDatetime.Ticks);
                //MonthDateTime = MonthDateTime.AddMonths(iMonthNumber);
                initLabelDate(MonthDateTime);
                monthDateTimeForWeek.Tag = currentDatetime.Ticks.ToString();
                monthDateTimeForWeek.SetDateTime(MonthDateTime);
            }
        }

        private void monthDateTimeForWeek_Load(object sender, EventArgs e)
        {

        }

        private void monthDateTimeForWeek_Load_1(object sender, EventArgs e)
        {
        }

        private void panShowMonth_Click(object sender, EventArgs e)
        {

        }

        private void MonthDateTimeForMonth_Load(object sender, EventArgs e)
        {
        }

    }
}
